package com.tiku.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeAppPay {

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCAZ6CYOk2WmIB0qycZG0vyyY2nDjTi93RtN7537JZtqyGm4KZSL4xOaovpnh23C30E4oTIXWPdbBGSYcGd12rx2JM5N1L0zooHK2p/9ZPcjgITtA00j9mjCQx7GbDwlrp6IxTbPZi/h5/12e73pnMYKpnFh0mWn3aHVWXmNQEJI7dUJ+CGvovIgU3tDOkelVwbldDAEUtELGr61n+26E3iUEKMqR5qW/LYIsQpC1JEPzov+yiXnTzdEjW2WLX2aHjBdybTUA8TXrhs3uiJg68ulRTnqsHBgB07r4591+rFw7AUocl624doVoSl05iiFSsJ0kSQ3CvZxbv/R1PnKObvAgMBAAECggEASzprQESISH64Y6ISmgcADBJBx2nvWjoYIBTUpnYZ+86TPK3JQ8VPsnGB3zEfv6ofaMk9ehYc+BDkippzUQJwSZjVSGQ9WefXvjOVhbpfEoywctK+GdldbMNUaJq4q3vjbMM46OnBoBTZhrRT6Qu/ypzK0otcdb6fNtK1jJXpMWy76ywdSl0kCnu12azr95lzNKBbQgdVMVxQ9oV4q2+4bWZCkHHPUrGxaT3nrFnzLRuHKCnCQj4WNhY5Eu2RcukilsHytWYZ1IQFhLK0DifJFXrcLPwclFJ9X6ZY6UWM6PbQPC6IBh4QZ0vuAss1Z3u48MB/EmfwiH3wotlSIGz24QKBgQDqg0BCL+EkdNjnTtwqeVCpFReDcOvyvvqwNraCwKAXF1OwsF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8yOD+G6gex8/LPaU+EY1XJlvpwGlbCU74euR1JXLTxOOHpkYzlvKYi1+2dnSc5ZsJMbdkKA0FjqWdcqxtP/R7+TgO0WpONJXCuNpI6iWXd7AocrWvOwuY13oCZSqn77GIBHPjPB9ZsdFzyg3Q9xTfGdvZNp86TnI2HEPrZxCwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000126654856");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setOutTradeNo("70501111111S001111119");
        model.setTotalAmount("9.00");
        model.setSubject("大乐透");
        request.setBizModel(model);
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        String orderStr = response.getBody();
        System.out.println(orderStr);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}